/**
 * @date 9:18:00 AM
 * EducationGroupDaoImpl.java
 * Administrator
 * TODO
 */
package com.qs.education.bean.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.qs.education.bean.beans.EducationGroupBean;
import com.qs.education.bean.dao.IEducationGroupDao;
import com.qs.education.bean.entity.EducationGroup;

/**
 * @class EducationGroupDaoImpl
 * @author Warren
 * @description
 * @date Nov 9, 2018
 */
@Repository
public class EducationGroupDaoImpl extends BaseDaoImpl<EducationGroup> implements IEducationGroupDao {

	@Override
	public List<EducationGroup> findEducationGroupByTeacherId(Integer teacherId) {
		String jql = "Select ea from EducationGroup ea where ea.teacher.id =:teacherId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("teacherId", teacherId);
		List<EducationGroup> groups = query(jql, EducationGroup.class, params);
		return groups;
	}


	@Override
	public int getGroupCount(String schoolName) {
		
		String jql = "Select COUNT(*) from EducationGroup eg ,EducationTeacher et , EducationSchool es "
				+ "where eg.teacher.id = et.id "
				+ "and et.school.id = es.id "
				+ "and es.name like :schoolName";
		Map<String, Object> params = new HashMap<String, Object>();
		if (schoolName !=null && !schoolName.trim().equals("")) {
			params.put("schoolName", "%"+schoolName+"%");
		}else {
			params.put("schoolName", "%%");
		}
		List<?> result = query(jql,params);
		if (result!=null && !result.isEmpty()) {
			return Integer.parseInt(result.get(0).toString());
		}
		
		return 0;
	}

	@Override
	public List<EducationGroup> getGroupList(String schoolName, Integer currentPage, Integer pageSize) {

		String jql = "Select eg from EducationGroup eg ,EducationTeacher et , EducationSchool es "
				+ "where eg.teacher.id = et.id "
				+ "and et.school.id = es.id "
				+ "and es.name like :schoolName";
		Map<String, Object> params = new HashMap<String, Object>();
		if (schoolName !=null && !schoolName.trim().equals("")) {
			params.put("schoolName", "%"+schoolName+"%");
		}else {
			params.put("schoolName", "%%");
		}
		
		int firstResult = (currentPage - 1) * pageSize;
		List<EducationGroup> list = query(jql,EducationGroup.class,firstResult,pageSize,params);
		
		return list;
	}


	@Override
	public List<EducationGroup> getGroupsByIds(List<Long> ids) {
		String jql = "Select ea from EducationGroup ea where ea.id in (:ids)";
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("ids", ids);
		List<EducationGroup> list = query(jql, EducationGroup.class, params);
		return list;
	}
}
